﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using ApplicationGenerator.CommonWeb;
using ApplicationGenerator.Lists.BusinessLogic;

namespace ApplicationGenerator.Lists.Web
{
    public class ListFilterControl : BaseControl
    {
        private IList<FilterColumn> _filterColumns;
        private Button _button;

        private Dictionary<FilterColumn, TextBox> _columnMappings;

        public event EventHandler SearchClicked;

        public ListFilterControl(IList<FilterColumn> filterColumns)
        {
            _filterColumns = filterColumns;
            _columnMappings = new Dictionary<FilterColumn, TextBox>();
            _button = new Button();
            _button.Click += new EventHandler(_button_Click);
        }

        void _button_Click(object sender, EventArgs e)
        {
            if (SearchClicked != null)
            {
                SearchClicked(sender, e);
            }
        }

        public void CreateControls()
        {
            Table table = new Table();
            TableRow row = null;
            TableCell cell = null;

            foreach (var filterColumn in _filterColumns)
            {
                row = new TableRow();
                cell = new TableCell();

                Label label = new Label();
                label.Text = filterColumn.DisplayName;

                cell.Controls.Add(label);
                row.Cells.Add(cell);

                cell = new TableCell();

                TextBox textBox = new TextBox();
                _columnMappings[filterColumn] = textBox;

                cell.Controls.Add(textBox);
                row.Cells.Add(cell);
                table.Rows.Add(row);
            }

            row = new TableRow();
            cell = new TableCell();
            _button.Text = "Szukaj";
            cell.Controls.Add(_button);
            row.Cells.Add(cell);
            table.Rows.Add(row);

            Controls.Add(table);
        }

        private string GetFilterColumnValue(Column column)
        {
            FilterColumn actualColumn = null;
            foreach (var filterColumn in _filterColumns)
            {
                if (filterColumn.RelatedColumn == column)
                {
                    actualColumn = filterColumn;
                }
            }

            if (actualColumn == null)
            {
                AddErrorMessage("Podana kolumna nie istnieje na filtrze");
                return null;
            }

            return _columnMappings[actualColumn].Text;
        }

        public string GetWhereStatement()
        {
            string whereStatement = " where ";
            foreach (KeyValuePair<FilterColumn, TextBox> mapping in _columnMappings)
            {
                if (mapping.Value.Text.Trim().Length > 0)
                {
                    whereStatement += string.Format("{0} like '{1}' and ", mapping.Key.RelatedColumn.Name, mapping.Value.Text);
                }
            }
            whereStatement += " 1 = 1";
            return whereStatement;
        }

    }
}
